package com.Example;

import java.util.ArrayList;
import java.util.Objects;

import javax.swing.DebugGraphics;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.World;

public class BoxObjectManager implements ContactListener {
	static World world;
//	static final float BOX_TO_WORLD=60f;
//	static final float WORLD_TO_BOX=0.0166f;
	static final float BOX_TO_WORLD=1f;
	static final float WORLD_TO_BOX=1f;
	
	public ArrayList<BaseBoxObject> bodies;
	

	float accumulator;
	boolean isPaused;
	
	
	public BoxObjectManager(){
		world=new World(new Vector2(0,-20), true);
		world.setContactListener(this);
		bodies=new ArrayList<BaseBoxObject>();
		isPaused=false;
	}
	
	public static World GetWorld(){
		return world;
	}

	public static float ConvertToBox(float x){
		return x*WORLD_TO_BOX;
	}
	
	public static float ConvertToWorld(float x){
		return x*BOX_TO_WORLD;
	}
	
	public void Pause(){
		accumulator=0;
		isPaused=true;
	}
	
	public int GetNewObjectIndex(){
		return bodies.size();
	}
	
	public void AddObject(BaseBoxObject obj){
		bodies.add(obj);
	}
	
	public void Update(float dt){
		if(!isPaused){
			accumulator+=dt;
			while(accumulator>dt){
				world.step(GlobalSettings.BOX_STEP,GlobalSettings.VELOCITY_ITERATIONS, GlobalSettings.POSITION_ITERATIONS);
				accumulator-=GlobalSettings.BOX_STEP/10;
			}
		}
		
		for(BaseBoxObject bo:bodies){
			bo.Update(dt);
		}
	}
	
	public void Draw(SpriteBatch sp){
		for(BaseBoxObject bo:bodies){
			bo.Draw(sp);
		}
		
	}

	public void Resume(){
		accumulator=0;
		isPaused=false;
	}
	
	public void Dispose(){
		for(BaseBoxObject tbo:bodies){
			tbo.DestroyBody();
		}
		bodies.clear();
	}

	@Override
	public void beginContact(Contact contact) {
		//Gdx.app.log("collision", "collision happened");
		Fixture f1=contact.getFixtureA();
        Body b1=f1.getBody();
        
        Fixture f2=contact.getFixtureB();
        Body b2=f2.getBody();
        
        
        BoxUserData userData1 = (BoxUserData) b1.getUserData();
        BoxUserData userData2 = (BoxUserData) b2.getUserData();
if(userData1 !=null && userData2 !=null)
{
        if((userData1.BodyCollisionGroup == GlobalSettings.ENEMY_GROUP)&&(userData2.BodyCollisionGroup == GlobalSettings.PIGEON_GROUP)){
        	userData1.setDead(true);
        	HandleEnemyColliedPigoen(userData2,userData1);
        
        }
        if((userData2.BodyCollisionGroup == GlobalSettings.ENEMY_GROUP)&&(userData1.BodyCollisionGroup == GlobalSettings.PIGEON_GROUP)){
        	HandleEnemyColliedPigoen(userData1,userData2);	
        	userData2.setDead(true);

        }
	}    
        
	}
   private void HandleEnemyColliedPigoen(BoxUserData pigeon_userdata,BoxUserData enemy_userData){
	 Gdx.app.log("collision", "collision happened");
	 
//	 BoxPigeon pigeon =(BoxPigeon) this.bodies.get(pigeon_userdata.GetIndex());
	 BoxPigeon.health-=10;
	 Gdx.app.log("healthbbbbbbbbbbbbbbbbb", BoxPigeon.health+"");
	 
	 //               Object1 object1=(Object1)objects.get(userData1.GetBoxID());
//               Object2 object2=(Object2)objects.get(userData2.GetObjectID());
//               //Change health/color etc of object1/2
//              //Maybe then add object2 for deletion
//               AddForDeletion(object2)
	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
		// TODO Auto-generated method stub
		
	}
}
